//
//  InboxAndPersonalViewController.m
//  WEIMO
//
//  Created by Lixiaoyong on 13-6-18.
//  Copyright (c) 2013年 广州从兴电子开发有限公司. All rights reserved.
//

#import "InboxAndPersonalViewController.h"

#import "FMManager.h"
#import "InboxViewCtrl.h"
#import "PersonalMessageViewController.h"
#import "DLog.h"
#import "KeyValueObject.h"
#import "MKNumberBadgeView.h"

@interface InboxAndPersonalViewController ()
@property (nonatomic,retain) UITableView *listTableView;
//<KeyValueObject:key-1(收件箱)/2(私信)、keydispname-名称、value-数量、valuedispname图片>
@property (nonatomic,retain) NSMutableArray *listTableViewDatas;

@property (nonatomic,retain) InboxViewCtrl *inboxCtrl;//收件箱
@property (nonatomic,retain) PersonalMessageViewController *personalFriendLists;//私信列表
@end

@implementation InboxAndPersonalViewController
@synthesize inboxCtrl,personalFriendLists;
@synthesize listTableView;
@synthesize listTableViewDatas;

- (void)dealloc
{
    self.listTableView = nil;
    self.inboxCtrl = nil;
    self.personalFriendLists = nil;
    self.listTableViewDatas = nil;
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    //判断是否有userId，若无则跳转到登录界面
    NSString *userId = [[FMManager sharedInstance] userId];
    if (!userId || userId.length <= 0)
    {
        LoginViewController *loginCtrl = [[LoginViewController alloc] init];
        //loginCtrl.delegate = self;
        [loginCtrl initWithStyle:UITableViewStyleGrouped];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
        [loginCtrl release];
        UIImage *navImage = [UIImage imageNamed:@"top.png"];
        if ([nav.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
        {
            [nav.navigationBar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
        }
        else
        {
            UIImageView* imageView = [[[UIImageView alloc] initWithFrame:nav.navigationBar.frame] autorelease];
            imageView.contentMode = UIViewContentModeLeft;
            imageView.image = navImage;
            [nav.navigationBar insertSubview:imageView atIndex:0];
        }
        
        [self.navigationController presentModalViewController:nav animated:YES];
        [nav release];
    }
}

- (id)init
{
    self = [super init];
    if (self)
    {
        self.title = @"工作圈";
        self.tabBarItem.image = [UIImage imageNamed:@"inbox"];
        
        self.view.backgroundColor = [UIColor colorWithRed:236.0/255 green:237.0/255 blue:241.0/255 alpha:1];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    float width = self.view.frame.size.width;
    float height= self.view.frame.size.height - 49 - 44;
    CGRect rect = CGRectMake(0, 0, width, height);
    self.listTableView = [[[UITableView alloc] initWithFrame:rect style:UITableViewStyleGrouped] autorelease];
    self.listTableView.backgroundView = nil;
    self.listTableView.backgroundColor = [UIColor colorWithRed:231/255.0 green:232/255.0 blue:236/255.0 alpha:1.0];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    self.listTableView.scrollEnabled = NO;
    self.listTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.listTableView];
    
    if (self.listTableViewDatas == Nil)
    {
        [self initListDatas];
    }
}

- (void)didReceiveMemoryWarning
{
    DEnter();
    [super didReceiveMemoryWarning];
}

#pragma mark - private methods

//初始化数据
- (void)initListDatas
{
    self.listTableViewDatas = [[[NSMutableArray alloc] init] autorelease];
    
    {
        KeyValueObject *item1 = [[KeyValueObject alloc] init];
        item1.key = @"1";
        item1.keyDispName = @"资料收件箱";
        item1.value = @"0";
        item1.valueDispName = @"shoujianxiang.png";
        [self.listTableViewDatas addObject:item1];
        [item1 release];
    }
    
    {
        KeyValueObject *item2 = [[KeyValueObject alloc] init];
        item2.key = @"2";
        item2.keyDispName = @"私信";
        item2.value = @"0";
        item2.valueDispName = @"sixin.png";
        [self.listTableViewDatas addObject:item2];
        [item2 release];
    }
}

#pragma mark - button event

- (void)firstBtnEventGotoInbox
{
    if (self.inboxCtrl == nil)
    {
        self.inboxCtrl = [[[InboxViewCtrl alloc] init] autorelease];
    }
    [self.navigationController pushViewController:self.inboxCtrl animated:YES];
}

- (void)secondBtnEventGotoPersonalMessageLists
{
    if (self.personalFriendLists == nil)
    {
        self.personalFriendLists = [[[PersonalMessageViewController alloc] init] autorelease];
    }
    
    [self.navigationController pushViewController:self.personalFriendLists animated:YES]; 
}

#pragma mark - protected methods

//刷箱条数
//flag:1--新资料收件  2-私信列表
- (void)refreshPersonalInboxNumbers:(int)number withFlag:(int)flag
{
    if (self.listTableViewDatas == nil)
    {
        [self initListDatas];
    }

    if (self.listTableViewDatas.count > 0)
    {
        if (flag == 1)
        {
            KeyValueObject *item = [self.listTableViewDatas objectAtIndex:0];
            item.value = [NSString stringWithFormat:@"%d",number];
            [self.listTableViewDatas replaceObjectAtIndex:0 withObject:item];
        }
        else
        {
            KeyValueObject *item = [self.listTableViewDatas objectAtIndex:1];
            item.value = [NSString stringWithFormat:@"%d",number];
            [self.listTableViewDatas replaceObjectAtIndex:1 withObject:item];
        }
        
        if (self.listTableView)
        {
            [self.listTableView reloadData];
        }
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listTableViewDatas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *listCellIdentifier = @"listCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listCellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listCellIdentifier] autorelease];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        float oX = cell.frame.size.width - 70.0;
        float oY = (cell.frame.size.height - 30) / 2.0;
        CGRect rect = CGRectMake(oX, oY, 30, 30);
        MKNumberBadgeView *numberView = [[[MKNumberBadgeView alloc] initWithFrame:rect] autorelease];
        numberView.shadow = NO;
        numberView.userInteractionEnabled = NO;
        numberView.hideWhenZero = YES;
        numberView.tag = 1001;
        [cell addSubview:numberView];
    }
    
    KeyValueObject *item = [self.listTableViewDatas objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:item.valueDispName];
    cell.textLabel.text = item.keyDispName;
    MKNumberBadgeView *numberView = (MKNumberBadgeView *)[cell viewWithTag:1001];
    numberView.value = item.value.intValue;
    
    DLog(@"item:%@,%@,%@",item.keyDispName,item.value,item.valueDispName);
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        [self firstBtnEventGotoInbox];
    }
    else
    {
        [self secondBtnEventGotoPersonalMessageLists];
    }
}

@end